package com.jeesite.modules.dataScreen.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.dataScreen.entity.*;
import com.jeesite.modules.dataScreen.service.EventConductService;
import com.jeesite.modules.dataScreen.utils.YgjyHelper;
import com.jeesite.modules.dataScreen.web.vo.GongluVo;
import com.jeesite.modules.dataScreen.web.vo.NameValueVo;
import com.jeesite.modules.dataScreen.web.vo.Result;
import com.jeesite.modules.dataScreen.web.vo.event.EventConductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: zhaosenlin
 * @date: 2024/5/14 14:08
 */
@RestController
@Api(tags = "公路接口")
@RequestMapping("/gonglu")
@Slf4j
public class GongluApi extends BaseController {

    @Resource
    EventConductService eventConductService;

    @ApiOperation("公路-运行设备")
    @GetMapping("yxsb")
    public Result<GongluVo.Yxsb> yxsb() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-基础设施");
        GongluVo.Yxsb j = new GongluVo.Yxsb();
        fillDataByTiaoBao(allData(), GongluVo.Yxsb.class, j);

        j.setValue55(getTianbaoValue("视频监控数", "高速集团").orElse("0"));
        // j.setValue56(getTianbaoValue("巡查车辆数", "高速集团").orElse("0"));
        j.setValue53(getTianbaoValue("ETC车道数").orElse("0"));
        j.setValue68(getTianbaoValue("ETC门架数").orElse("0"));
        j.setValue52(getTianbaoValue("可变情报板数").orElse("0"));
        j.setValue54(getTianbaoValue("高速公路收费站数量").orElse("0"));

        GongluVo.H.HH value51 = new GongluVo.H.HH();
        j.setValue1(getTianbaoValue("视频监控数量", "公路中心").orElse("0"));
        j.setValue220(getTianbaoValue("交调站数量").orElse("0"));


        j.setValuejq(String.valueOf(themeDbDao.gl_jqz(Page.of(1, 1)).getTotal()));
        double cdz = NumberUtil.add(themeDbDao.gl_gdcdz(Page.of(1, 1)).getTotal(),
                                    themeDbDao.gl_ydcdz(Page.of(1, 1)).getTotal());
        j.setValuecdz(NumberUtil.toStr(cdz));
        double hdz = NumberUtil.add(themeDbDao.gl_hdz(Page.of(1, 1)).getTotal(),
                                    themeDbDao.gl_zkhdz(Page.of(1, 1)).getTotal());
        j.setValuehdz(NumberUtil.toStr(hdz));

        fillFieldInfo(j, fieldMean);
        return Result.OK(j).apiInfo(fieldMean);
    }

    @ApiOperation("公路-路网综合")
    @GetMapping("lwzh")
    public Result<GongluVo.Lwzh> lwzh() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-监测预警");
        GongluVo.Lwzh jcyj = new GongluVo.Lwzh();
        fillDataByTiaoBao(allData(), GongluVo.Lwzh.class, jcyj);

        fillFieldInfo(jcyj, fieldMean);
        return Result.OK(jcyj).apiInfo(fieldMean);
    }

    @ApiOperation("公路-监测预警")
    @GetMapping("jcyj")
    public Result<GongluVo.Jcyj> jcyj() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-监测预警");
        GongluVo.Jcyj jcyj = new GongluVo.Jcyj();
        fillDataByTiaoBao(allData(), GongluVo.Jcyj.class, jcyj);

        fillFieldInfo(jcyj, fieldMean);
        return Result.OK(jcyj).apiInfo(fieldMean);
    }

    @ApiOperation("公路-公路养护")
    @GetMapping("gljbz")
    public Result<GongluVo.K> gljbz() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-公路养护");
        GongluVo.K k = new GongluVo.K();
        fillDataByTiaoBao(allData(), GongluVo.K.class, k);
        return Result.OK(k).apiInfo(fieldMean);
    }

    @ApiOperation("公路-公路规划")
    @GetMapping("glgh")
    public Result<GongluVo.B> glgh() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-公路规划");
        GongluVo.B b = new GongluVo.B();
        fillDataByTiaoBao(allData(), GongluVo.B.class, b);
        // GongluVo.B.BB value18 = new GongluVo.B.BB();
        // // value18.setValue17(getTianbaoValue("当年计划-计划总投资").orElse("0"));
        // value18.setValue18(getTianbaoValue("高速公路当年规划").orElse("0"));
        // // value18.setValue19(getTianbaoValue("当年计划-农村公路").orElse("0"));
        // value18.setValue20(getTianbaoValue("普通公路当年规划").orElse("0"));
        // value18.setValue17(NumberUtil.add(value18.getValue18(), value18.getValue20()).toString());
        // b.setValue14(value18);
        //
        // GongluVo.B.BB value19 = new GongluVo.B.BB();
        // // value19.setValue17(getTianbaoValue("五年规划-计划总投资").orElse("0"));
        // value19.setValue18(getTianbaoValue("高速公路五年规划").orElse("0"));
        // // value19.setValue19(getTianbaoValue("五年规划-农村公路").orElse("0"));
        // value19.setValue20(getTianbaoValue("普通公路五年规划").orElse("0"));
        // value19.setValue17(NumberUtil.add(value19.getValue18(), value19.getValue20()).toString());
        // b.setValue15(value19);
        //
        // GongluVo.B.BB value20 = new GongluVo.B.BB();
        // // value20.setValue17(getTianbaoValue("中长期规划-计划总投资").orElse("0"));
        // value20.setValue18(getTianbaoValue("高速公路中长期规划").orElse("0"));
        // value20.setValue19(getTianbaoValue("农村公路中长期规划").orElse("0"));
        // value20.setValue20(getTianbaoValue("国省干线中长期规划").orElse("0"));
        // value20.setValue17(NumberUtil.add(value20.getValue18(), value20.getValue19(), value20.getValue20()).toString());
        // b.setValue16(value20);
        return Result.OK(b).apiInfo(fieldMean);
    }

    @ApiOperation("公路-公路建设")
    @GetMapping("gljs")
    public Result<GongluVo.N> gljs() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-公路建设");
        GongluVo.N n = new GongluVo.N();
        fillDataByTiaoBao(allData(), GongluVo.N.class, n);

        return Result.OK(n).apiInfo(fieldMean);
    }

    // @ApiOperation("公路-在建高速项目列表")
    // @GetMapping("zjgsxmlb")
    // public Result<GongluVo.E> zjgsxmlb() {
    //     Map<String, Object> fieldMean = new LinkedHashMap<>();
    //     fieldMean.put("title", "公路-在建高速项目列表");
    //     return Result.OK(new GongluVo.E()).apiInfo(fieldMean);
    // }

    // @ApiOperation("公路-公路管养")
    // @GetMapping("glgy")
    // public Result<GongluVo.E> glgy() {
    //     Map<String, Object> fieldMean = new LinkedHashMap<>();
    //     fieldMean.put("title", "公路-公路管养");
    //     return Result.OK(new GongluVo.E()).apiInfo(fieldMean);
    // }

    // @ApiOperation("公路-路网运行")
    // @GetMapping("lwyn")
    // public Result<GongluVo.F> lwyn() {
    //     Map<String, Object> fieldMean = new LinkedHashMap<>();
    //     fieldMean.put("title", "公路-路网运行");
    //     return Result.OK(new GongluVo.F()).apiInfo(fieldMean);
    // }

    @ApiOperation("公路-路网通行")
    @GetMapping("lwtx")
    public Result<GongluVo.L> lwtx() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-路网通行");
        GongluVo.L l = new GongluVo.L();
        fillDataByTiaoBao(allData(), GongluVo.L.class, l);

        // List<NameValueVo> zdList = sjtbDbDao.windowListpugl("普通公路", "status0", String.valueOf(DateUtil.thisYear()));
        // CollUtil.sort(zdList, (o1, o2) -> CompareUtil.compare(
        //         NumberUtil.toBigDecimal(o2.getValue()),
        //         NumberUtil.toBigDecimal(o1.getValue()),
        //         false));
        // l.setZdTotal(0L);
        // for (NameValueVo nameValueVo : zdList) {
        //     Long v = NumberUtil.parseLong(nameValueVo.getValue(), 0L);
        //     l.setZdTotal(l.getZdTotal() + v);
        // }
        // CollUtil.filter(zdList, nameValueVo -> NumberUtil.parseLong(nameValueVo.getValue(), 0L) != 0);
        // l.setZdList(CollUtil.sub(zdList, 0, 5));
        //
        // List<NameValueVo> tfList = sjtbDbDao.windowListgs("高速公路", "status0", String.valueOf(DateUtil.thisYear()));
        // CollUtil.sort(tfList, (o1, o2) -> CompareUtil.compare(
        //         NumberUtil.toBigDecimal(o2.getValue()),
        //         NumberUtil.toBigDecimal(o1.getValue()),
        //         false));
        // l.setTfTotal(0L);
        // for (NameValueVo nameValueVo : tfList) {
        //     Long v = NumberUtil.parseLong(nameValueVo.getValue(), 0L);
        //     l.setTfTotal(l.getTfTotal() + v);
        // }
        // CollUtil.filter(tfList, nameValueVo -> NumberUtil.parseLong(nameValueVo.getValue(), 0L) != 0);
        // l.setTfList(CollUtil.sub(tfList, 0, 5));

        // List<NameValueVo> gs = sjtbDbDao.windowListgs("高速公路", "status1", String.valueOf(DateUtil.thisYear()));
        // List<NameValueVo> gs0 = sjtbDbDao.windowListpugl("普通公路", "status1", String.valueOf(DateUtil.thisYear()));
        // l.setGstfycl(gs.stream().map(NameValueVo::getValue).mapToLong(NumberUtil::parseLong).sum());
        // l.setGsdtfycl(gs0.stream().map(NameValueVo::getValue).mapToLong(NumberUtil::parseLong).sum());

        // JSONObject tzzbGsdfwsp = sjtbDbDao.tzzbGsdfwsp();
        // l.setValue126(tzzbGsdfwsp.getString("qczrs"));
        // l.setValue148(tzzbGsdfwsp.getString("qcdls"));
        // l.setValue149(tzzbGsdfwsp.getString("kczrs"));
        // l.setValue150(tzzbGsdfwsp.getString("hczrs"));
        // l.setValue151(tzzbGsdfwsp.getString("jdcdls"));
        // l.setValueYjd(tzzbGsdfwsp.getString("vc"));

        // l.setGsdqcdl(tzzbGsdfwsp.getString("qcdls"));
        // // 其他机动车当量=机动车当量数-汽车当量数
        // l.setGsdqtqcdl(NumberUtil.sub(tzzbGsdfwsp.getString("jdcdls"), l.getGsdqcdl()).toString());

        // Object putonggonglu = sjtbDbDao.putonggonglu();

        JSONObject sfdata = sjtbDbDao.sfdata();
        l.setValue210(sfdata.getString("msg_flow"));
        l.setValue211(sfdata.getString("msg_enflow"));
        l.setValue213(sfdata.getString("msg_exflow"));

        JSONObject pjsdhjtzs = sjtbDbDao.pjsdhjtzs();
        l.setValue212(ObjectUtil.defaultIfNull(pjsdhjtzs.getString("avgspeed"), "0"));

        if (l.getLine10() != null) {
            Object line10 = l.getLine10();
            l.setLine10(convert(line10));
        }

        return Result.OK(l).apiInfo(fieldMean);
    }

    public static List<JSONObject> convert(Object line10) {
        List<JSONObject> list = new ArrayList<>();

        JSONObject jo = JSONObject.parseObject(String.valueOf(line10));

        JSONArray data = jo.getJSONArray("data");
        data.forEach(new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                JSONArray arr = (JSONArray) o;
                JSONObject j = new JSONObject();
                j.put("name", arr.getString(2));
                j.put("value", arr.getString(3));
                list.add(j);
            }
        });
        // for (int i = 1; i < 11; i++) {
        //     JSONObject j = jo.getJSONObject(String.valueOf(i));
        //     j.put("name", j.getString("lxmc"));
        //     j.put("value", j.getString("jdcdls"));
        //     list.add(j);
        // }

        CollUtil.sort(list, (o1, o2) -> CompareUtil.compare(
                NumberUtil.toBigDecimal(o1.getString("value")),
                NumberUtil.toBigDecimal(o2.getString("value")),
                false));
        return list;
    }

    @ApiOperation("公路-运行设施")
    @GetMapping("glyn")
    public Result<GongluVo.H> glyn() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-运行设施");
        GongluVo.H h = new GongluVo.H();
        fillDataByTiaoBao(allData(), GongluVo.H.class, h);

        // JSONObject tzzbedXccgdsp = sjtbDbDao.tzzbXccgdsp();

        GongluVo.H.HH value50 = new GongluVo.H.HH();
        // value50.setValue55(tzzbedXccgdsp.getString("gdspsl"));
        // value50.setValue56(tzzbedXccgdsp.getString("xccsl"));
        value50.setValue55(getTianbaoValue("视频监控数", "高速集团").orElse("0"));
        value50.setValue56(getTianbaoValue("巡查车辆数", "高速集团").orElse("0"));
        value50.setValue53(getTianbaoValue("ETC车道数").orElse("0"));
        value50.setValue68(getTianbaoValue("ETC门架数").orElse("0"));
        value50.setValue52(getTianbaoValue("可变情报板数").orElse("0"));
        value50.setValue54(getTianbaoValue("高速公路收费站数量").orElse("0"));
        h.setValue50(value50);

        GongluVo.H.HH value51 = new GongluVo.H.HH();
        value51.setValue56(getTianbaoValue("巡察车数量", "公路中心").orElse("0"));
        value51.setValue55(getTianbaoValue("视频监控数量", "公路中心").orElse("0"));
        value51.setValue220(getTianbaoValue("交调站数量").orElse("0"));

        h.setValue51(value51);
        return Result.OK(h).apiInfo(fieldMean);
    }

    // @ApiOperation("公路-服务荣耀")
    // @GetMapping("fwry")
    // public Result<GongluVo.I> fwry() {
    //     Map<String, Object> fieldMean = new LinkedHashMap<>();
    //     fieldMean.put("title", "公路-服务荣耀");
    //     return Result.OK(new GongluVo.I()).apiInfo(fieldMean);
    // }

    @ApiOperation("公路-高速公路救援")
    @GetMapping("gsgljy")
    public Result<GongluVo.O> gsgljy() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-高速公路救援");
        GongluVo.O o = new GongluVo.O();
        fillDataByTiaoBao(allData(), GongluVo.O.class, o);

        JSONObject rescuestationinfo = sjtbDbDao.rescuestationinfo();
        o.setValue241("");
        o.setValue201(safeGet(rescuestationinfo, jo -> jo.getString("rescuestation"), "0"));
        o.setValue202(safeGet(rescuestationinfo, jo -> jo.getString("rescuestation"), "0"));
        JSONObject dailyrescuedata = sjtbDbDao.dailyrescuedata();
        o.setValue204(safeGet(dailyrescuedata, jo -> jo.getString("numberofdailyrescues"), "0"));
        o.setValue203(safeGet(dailyrescuedata, jo -> jo.getString("complete"), "0"));
        o.setValue207(safeGet(dailyrescuedata, jo -> jo.getString("suspend"), "0"));
        o.setValue206(safeGet(dailyrescuedata, jo -> jo.getString("inprogress"), "0"));
        JSONObject rescuecarnum = sjtbDbDao.rescuecarnum();
        o.setValue205(safeGet(rescuecarnum, jo -> jo.getString("vehiclescount"), "0"));
        o.setValue209(safeGet(rescuecarnum, jo -> jo.getString("currentonlinevehicles"), "0"));
        o.setValue208(safeGet(rescuecarnum, jo -> jo.getString("currentofflinevehicles"), "0"));

        return Result.OK(o).apiInfo(fieldMean);
    }

    @ApiOperation("公路-出行服务")
    @GetMapping("cxfw")
    public Result<GongluVo.M> cxfw() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-出行服务");
        GongluVo.M m = new GongluVo.M();
        fillDataByTiaoBao(allData(), GongluVo.M.class, m);

        String day = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -1), "yyyyMMdd");
        // day = "20230520";

        Long alarmCount = sjtbDbDao.alarm12122(day);
        Long alarm12122NotCalled = sjtbDbDao.alarm12122NotCalled(day);
        m.setValue137(alarmCount);
        m.setValue138(CompareUtil.compare(alarmCount, 0L) > 0 ?
                              NumberUtil.toStr(new BigDecimal("100")
                                                       .subtract(NumberUtil.mul(NumberUtil.div(
                                                                                        NumberUtil.toBigDecimal(alarm12122NotCalled),
                                                                                        NumberUtil.toBigDecimal(alarmCount), 4, RoundingMode.HALF_UP),
                                                                                new BigDecimal("100")))
                              )
                              : "0");

        List<NameValueVo> alarm12122Classification = sjtbDbDao.alarm12122Classification(day);
        SortedMap<String, Long> sortedMap = new TreeMap<>(Comparator.reverseOrder());
        alarm12122Classification.forEach(nameValueVo -> {
            List<String> split = StrUtil.split(nameValueVo.getName(), ',');
            String first = CollUtil.getFirst(split);
            sortedMap.computeIfAbsent(first, s -> 0L);
            sortedMap.computeIfPresent(first, (s, aLong) -> {
                Long l = NumberUtil.parseLong(nameValueVo.getValue(), 0L);
                return aLong + l;
            });
        });
        // 使用Stream API按值降序排序并取前三
        List<NameValueVo> topThreeEntries = sortedMap.entrySet().stream()
                // 显式按值降序排序
                .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
                // .limit(3) // 取前3
                .map(new Function<Map.Entry<String, Long>, NameValueVo>() {
                    @Override
                    public NameValueVo apply(Map.Entry<String, Long> stringLongEntry) {
                        NameValueVo nameValueVo = new NameValueVo();
                        nameValueVo.setName(stringLongEntry.getKey());
                        nameValueVo.setValue(String.valueOf(stringLongEntry.getValue()));
                        return nameValueVo;
                    }
                })
                .collect(Collectors.toList());
        m.setTotal(((Long) sortedMap.values().stream().mapToLong(value -> value).sum()).toString());
        m.setTop3(topThreeEntries);

        return Result.OK(m).apiInfo(fieldMean);
    }

    @ApiOperation("公路-高速公路实时交通量")
    @GetMapping("gsglssjtl")
    public Result<GongluVo.P> gsglssjtl() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-高速公路实时交通量");
        GongluVo.P p = new GongluVo.P();
        JSONObject sfdata = sjtbDbDao.sfdata();
        p.setValue210(sfdata.getString("msg_flow"));
        p.setValue211(sfdata.getString("msg_enflow"));
        p.setValue213(sfdata.getString("msg_exflow"));

        JSONObject pjsdhjtzs = sjtbDbDao.pjsdhjtzs();
        p.setValue212(ObjectUtil.defaultIfNull(pjsdhjtzs.getString("avgspeed"), "0"));

        return Result.OK(p).apiInfo(fieldMean);
    }

    @ApiOperation("公路-公路现状")
    @GetMapping("glxz")
    public Result<GongluVo.Q> glxz() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-公路现状");
        GongluVo.Q q = new GongluVo.Q();
        fillDataByTiaoBao(allData(), GongluVo.Q.class, q);

        JSONObject tzzbedGsdlkgsd = sjtbDbDao.tzzbGsdlk("gsd");
        q.setValue188(tzzbedGsdlkgsd.getString("pqipjz"));

        JSONObject tzzbNcgllk = sjtbDbDao.tzzbNcgllk("ncgl");
        q.setValue189(tzzbNcgllk.getString("pqipjz"));

        return Result.OK(q).apiInfo(fieldMean);
    }

    @ApiOperation("公路-基础设施")
    @GetMapping("yxss")
    public Result<GongluVo.J> yxss() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "公路-基础设施");
        GongluVo.J j = new GongluVo.J();
        fillDataByTiaoBao(allData(), GongluVo.J.class, j);

        // j.setValue144(NumberUtil.add(j.getValue159(), j.getValue160(), j.getValue161(), j.getValue162()).toString());
        // j.setValue3(NumberUtil.add(j.getValue159(), j.getValue160()).toString());

        j.setValue148(getTianbaoValue("特长隧道", "高速集团").orElse("0"));
        j.setValue149(getTianbaoValue("长隧道", "高速集团").orElse("0"));
        j.setValue150(getTianbaoValue("中隧道", "高速集团").orElse("0"));
        j.setValue151(getTianbaoValue("短隧道", "高速集团").orElse("0"));
        // j.setValue147(NumberUtil.add(j.getValue148(), j.getValue149(), j.getValue150(), j.getValue151()).toString());
        // j.setValue7(NumberUtil.add(j.getValue148(), j.getValue149()).toString());


        JSONObject tzzbedQlgsd = sjtbDbDao.tzzbQl("gsd");
        // j.setValue69(tzzbedQlgsd.getString("qlzsl"));
        // j.setValue70(tzzbedQlgsd.getString("tdqsl"));
        // j.setValue71(tzzbedQlgsd.getString("dqsl"));
        // j.setValue72(tzzbedQlgsd.getString("zqsl"));
        // j.setValue73(tzzbedQlgsd.getString("xqsl"));
        // // Integer silslqlgsd = nullToZero(tzzbedQlgsd.getInteger("silsl"));
        // // Integer wlslqlgsd  = nullToZero(tzzbedQlgsd.getInteger("wlsl"));
        // // j.setValue74(silslqlgsd + wlslqlgsd);
        // j.setValue74((int) sjtbDbDao.base_glql(Page.of(1, 1), 1).getTotal());
        // j.setValue75(tzzbedQlgsd.getString("swlqlzb"));

        JSONObject tzzbedQlncgl = sjtbDbDao.tzzbQl("ncgl");
        // j.setValue76(tzzbedQlncgl.getString("qlzsl"));
        // j.setValue216(tzzbedQlncgl.getString("tdqsl"));
        // j.setValue217(tzzbedQlncgl.getString("dqsl"));
        // j.setValue218(tzzbedQlncgl.getString("zqsl"));
        // j.setValue219(tzzbedQlncgl.getString("xqsl"));
        Integer silslqlncgl = nullToZero(tzzbedQlncgl.getInteger("silsl"));
        Integer wlslqlncgl = nullToZero(tzzbedQlncgl.getInteger("wlsl"));
        j.setValue77(silslqlncgl + wlslqlncgl);
        j.setValue78(tzzbedQlncgl.getString("swlqlzb"));
        // j.setValuewq((int) sjtbDbDao.base_glql(Page.of(1, 1), 2).getTotal());

        // j.setValue14(NumberUtil.add(j.getValue216(), j.getValue217()).toString());
        // j.setValue15(ComputeUtil.divToStr(j.getValue14(), j.getValue76()));

        JSONObject tzzbedSdgsd = sjtbDbDao.tzzbSd("gsd");
        // j.setValue79(tzzbedSdgsd.getString("sdzsl"));
        j.setValue80(tzzbedSdgsd.getString("tcsdsl"));
        j.setValue81(tzzbedSdgsd.getString("csdsl"));
        // j.setValue82(tzzbedSdgsd.getString("zsdsl"));
        // j.setValue83(tzzbedSdgsd.getString("dsdsl"));

        // j.setValue16(NumberUtil.add(j.getValue80(), j.getValue81()).toString());

        Integer silsl = nullToZero(tzzbedSdgsd.getInteger("silsl"));
        Integer wlsl = nullToZero(tzzbedSdgsd.getInteger("wlsl"));
        // j.setValue84((int) sjtbDbDao.base_glsd(Page.of(1, 1), 1).getTotal());
        // j.setValue85(tzzbedSdgsd.getString("swlsdzb"));

        JSONObject tzzbedSdncgl = sjtbDbDao.tzzbSd("ncgl");
        // j.setValue208(tzzbedSdncgl.getString("sdzsl"));
        // j.setValue209(tzzbedSdncgl.getString("tcsdsl"));
        // j.setValue210(tzzbedSdncgl.getString("csdsl"));
        // j.setValue211(tzzbedSdncgl.getString("zsdsl"));
        // j.setValue212(tzzbedSdncgl.getString("dsdsl"));
        Integer silslncgl = nullToZero(tzzbedSdncgl.getInteger("silsl"));
        Integer wlslncgl = nullToZero(tzzbedSdncgl.getInteger("wlsl"));
        // j.setValue213(silslncgl + wlslncgl);
        // j.setValue214(tzzbedSdncgl.getString("swlsdzb"));

        JSONObject tzzbFwssVo = sjtbDbDao.tzzbFwss();
        // j.setFwsszs(tzzbFwssVo.getInteger("fwsszs"));
        // j.setGdsl(tzzbFwssVo.getDouble("gdsl"));
        // j.setSdsl(tzzbFwssVo.getDouble("sdsl"));
        // j.setNcglsl(tzzbFwssVo.getDouble("ncglsl"));

        // j.setValuejq(String.valueOf(themeDbDao.gl_jqz(Page.of(1, 1)).getTotal()));
        // double cdz = NumberUtil.add(themeDbDao.gl_gdcdz(Page.of(1, 1)).getTotal(),
        //         themeDbDao.gl_ydcdz(Page.of(1, 1)).getTotal());
        // j.setValuecdz(NumberUtil.toStr(cdz));
        // double hdz = NumberUtil.add(themeDbDao.gl_hdz(Page.of(1, 1)).getTotal(),
        //         themeDbDao.gl_zkhdz(Page.of(1, 1)).getTotal());
        // j.setValuehdz(NumberUtil.toStr(hdz));
        j.setValuesjzj(String.valueOf(themeDbDao.gl_sjzj(Page.of(1, 1)).getTotal()));

        return Result.OK(j).apiInfo(fieldMean);
    }

    @ApiOperation("高速公路事件")
    @GetMapping("gsglsj")
    public Result<Page<Tufashijian>> gsglsj(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Long page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit,
            @ApiParam("道路类型 高速1 国省道2") @RequestParam(required = false, defaultValue = "1") Integer roadType) {
        // Page<Tufashijian> tufashijian = themeDbDao.tufashijian(Page.of(page, limit));
        return gsglsjxq01(sjtbDbDao.tfsj(Page.of(page, limit), null, roadType));
        // return gsglsjxq01(sjtbDbDao.tfsj2(Page.of(page, limit), String.valueOf(DateUtil.thisYear()), null, roadType));
    }

    @ApiOperation("高速公路事件事件详情")
    @GetMapping("gsglsjxq")
    public Result<Page<Tufashijian>> gsglsjxq(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Long page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit,
            @ApiParam("事件标题") @RequestParam String sjbt) {
        return gsglsjxq01(sjtbDbDao.tfsj(Page.of(page, limit), sjbt, null));
        // return gsglsjxq01(sjtbDbDao.tfsj2(Page.of(page, limit), String.valueOf(DateUtil.thisYear()), sjbt, null));
    }

    private Result<Page<Tufashijian>> gsglsjxq01(Page<Tufashijian> sjtbDbDao) {
        Page<Tufashijian> tufashijian = sjtbDbDao;
        tufashijian.getRecords().forEach(new Consumer<Tufashijian>() {
            @Override
            public void accept(Tufashijian tufashijian) {
                tufashijian.setShijian(tufashijian.getShijian());
                tufashijian.setLuduan(tufashijian.getRoadname());
                tufashijian.setXiangqing(tufashijian.getSjgk());
                tufashijian.setZhuangtai(StrUtil.equals("0", tufashijian.getCzzt()) ? "正在处理" : "已处置");
                tufashijian.setZhuangtai(StrUtil.isNotBlank(tufashijian.getJssjms()) ? "办结" : tufashijian.getZhuangtai());
            }
        });
        return Result.OK(tufashijian);
    }

    @ApiOperation("国省道事件")
    @GetMapping("gsdsj")
    public Result<Page<Zuduan60>> gsdsj(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return gsdsj01(sjtbDbDao.zdsj(Page.of(page, limit), null));
    }

    @ApiOperation("国省道事件详情")
    @GetMapping("gsdsjxq")
    public Result<Page<Zuduan60>> gsdsjxq(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit,
            @ApiParam("事件标题") @RequestParam String sjbt) {
        // Page<Zuduan60> zuduan60 = themeDbDao.zuduan60(Page.of(page, limit));
        return gsdsj01(sjtbDbDao.zdsj(Page.of(page, limit), sjbt));
    }

    private Result<Page<Zuduan60>> gsdsj01(Page<Zuduan60> sjtbDbDao) {
        // Page<Zuduan60> zuduan60 = themeDbDao.zuduan60(Page.of(page, limit));
        Page<Zuduan60> zuduan60 = sjtbDbDao;
        zuduan60.getRecords().forEach(new Consumer<Zuduan60>() {
            @Override
            public void accept(Zuduan60 zuduan60) {
                zuduan60.setShijian(zuduan60.getShijian());
                zuduan60.setDidian(zuduan60.getSsld());
                zuduan60.setXiangqing(zuduan60.getSjms());
                zuduan60.setZhuangtai(StrUtil.equals("0", zuduan60.getSjzt()) ? "正在处理" : "已处置");
            }
        });
        return Result.OK(zuduan60);
    }

    @ApiOperation("收费站拥堵")
    @GetMapping("sfzyd")
    public Result<Page<Yongdushijian>> sfzyd(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        // Page<Yongdushijian> yongdushijian = themeDbDao.yongdushijian(Page.of(page, limit));
        return sfzyd01(sjtbDbDao.ydsj(Page.of(page, limit), null));
    }

    @ApiOperation("收费站拥堵详情")
    @GetMapping("sfzydxq")
    public Result<Page<Yongdushijian>> sfzydxq(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit,
            @ApiParam("事件标题") @RequestParam String sjbt) {
        // Page<Yongdushijian> yongdushijian = themeDbDao.yongdushijian(Page.of(page, limit));
        return sfzyd01(sjtbDbDao.ydsj(Page.of(page, limit), sjbt));
    }

    private Result<Page<Yongdushijian>> sfzyd01(Page<Yongdushijian> yongdushijian) {
        yongdushijian.getRecords().forEach(new Consumer<Yongdushijian>() {
            @Override
            public void accept(Yongdushijian yongdushijian) {
                yongdushijian.setShijian(yongdushijian.getSjbt());
                yongdushijian.setLuduan(yongdushijian.getRoadname());
                yongdushijian.setXiangqing(yongdushijian.getSjgk());
                yongdushijian.setZhuangtai(StrUtil.equals("0", yongdushijian.getCzzt()) ? "正在处理" : "已处置");
                yongdushijian.setZhuangtai(StrUtil.isNotBlank(yongdushijian.getJssjms()) ? "办结" : yongdushijian.getZhuangtai());
            }
        });
        return Result.OK(yongdushijian);
    }

    @ApiOperation("高速公路建设项目投资计划表")
    @GetMapping("tcjhb")
    public Result<Page<GongluVo.Jhb>> jhb(
            @ApiParam("数据类型 1：新开工项目 2：建成项目 3：在建项目") @RequestParam Integer type,
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return Result.OK(new Page<GongluVo.Jhb>().setRecords(new ArrayList<>()));
    }

    @ApiOperation("换电站信息统计")
    @GetMapping("tchdz")
    public Result<Page<Hdz>> hdz(
            @ApiParam("数据类型 1：换电站 2：重卡换电站") @RequestParam Integer type,
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Hdz> cdzPage = new Page<>(page, limit, 0);
        if (Objects.equals(1, type)) {
            cdzPage = themeDbDao.gl_hdz(Page.of(page, limit));
        }
        if (Objects.equals(2, type)) {
            cdzPage = themeDbDao.gl_zkhdz(Page.of(page, limit));
        }
        return Result.OK(cdzPage);
    }

    @ApiOperation("加气站信息")
    @GetMapping("tcjqz")
    public Result<Page<Jqz>> jqz(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Jqz> jqzPage = themeDbDao.gl_jqz(Page.of(page, limit));
        return Result.OK(jqzPage);
    }

    @ApiOperation("司机之家信息")
    @GetMapping("tcsjzj")
    public Result<Page<Sjzj>> sjzj(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Sjzj> sjzjPage = themeDbDao.gl_sjzj(Page.of(page, limit));
        return Result.OK(sjzjPage);
    }

    @ApiOperation("充电桩信息")
    @GetMapping("tccdz")
    public Result<Page<Cdz>> cdz(
            @ApiParam("数据类型 1：固定充电桩 2：移动充电桩") @RequestParam Integer type,
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Cdz> cdzPage = new Page<>(page, limit, 0);
        if (Objects.equals(1, type)) {
            cdzPage = themeDbDao.gl_gdcdz(Page.of(page, limit));
        }
        if (Objects.equals(2, type)) {
            cdzPage = themeDbDao.gl_ydcdz(Page.of(page, limit));
        }
        return Result.OK(cdzPage);
    }

    @ApiOperation("车流量数据详情")
    @GetMapping("tccll")
    public Result<Page<Cll>> cll(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        LocalDate target = LocalDate.now().minusMonths(1);
        Page<Cll> cllPage = sjtbDbDao.tzzb_gsdyjdpm(Page.of(page, limit), target.getYear(), target.getMonth().getValue());
        return Result.OK(cllPage);
    }

    @ApiOperation("在建高速列表")
    @GetMapping("tczjgslb")
    public Result<Page<Zjgs>> tczjgslb(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Zjgs> zjgs = themeDbDao.zjgs(Page.of(page, limit));
        return Result.OK(zjgs);
    }

    @ApiOperation("危险桥梁")
    @GetMapping("tcwxql")
    public Result<Page<Wxql>> wxql(
            @ApiParam("类型 1 国省 2 农村") @RequestParam(required = false, defaultValue = "1") Integer type,
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Wxql> zjgs = sjtbDbDao.base_glql(Page.of(page, limit), type);
        return Result.OK(zjgs);
    }

    @ApiOperation("危险隧道")
    @GetMapping("tcwxsd")
    public Result<Page<Wxsd>> wxsd(
            @ApiParam("类型 1 国省 2 农村") @RequestParam(required = false, defaultValue = "1") Integer type,
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Wxsd> zjgs = sjtbDbDao.base_glsd(Page.of(page, limit), type);
        return Result.OK(zjgs);
    }

    @ApiOperation("阳光救援获取token")
    @GetMapping("ygjytoken")
    public Result<String> ygjytoken() {
        return YgjyHelper.getToken();
    }

    @GetMapping("/eventList")
    @ApiOperation("事件处置-弹窗列表")
    public Result<IPage<EventConductVo>> windowList(@RequestParam(defaultValue = "1") int pageNum,
                                                    @RequestParam(defaultValue = "10") int pageSize,
                                                    @RequestParam(required = false, defaultValue = "普通公路") String mainType,
                                                    @RequestParam(defaultValue = "allStatus") String status
    ) {
        if (StrUtil.equals("高速公路", mainType)) {
            IPage<EventConductVo> eventConductVoIPage = sjtbDbDao.windowListGaosu(Page.of(pageNum, pageSize),
                                                                                  mainType,
                                                                                  status,
                                                                                  String.valueOf(DateUtil.thisYear()));
            return Result.OK(eventConductVoIPage);
        }

        IPage<EventConductVo> eventConductVoIPage = sjtbDbDao.windowList(Page.of(pageNum, pageSize),
                                                                         mainType,
                                                                         status,
                                                                         String.valueOf(DateUtil.thisYear()));
        return Result.OK(eventConductVoIPage);
    }
}
